package com.mit.service.purchase.dao.impl;

import java.util.HashMap;
import java.util.List;

import com.mit.common.exception.NoRecordUpdateException;
import com.mit.common.purchase.dto.SupplierDTO;
import com.mit.common.token.PurchaseTokens;
import com.mit.common.token.Tokens;
import com.mit.service.core.dao.AbstractDAO;
import com.mit.service.purchase.dao.ISupplierDAO;

public class SupplierDAOImpl extends AbstractDAO implements ISupplierDAO {

	@Override
	public SupplierDTO createSupplierDTO(SupplierDTO to) {
		Integer id = (Integer) getSqlMapClientTemplate().insert("insertSupplier", to);
		to.setId(id);
		return to;
	}

	@Override
	public List<SupplierDTO> retrievePagedSuppliers(Integer orgId, String searchKey, Integer pageNo, Integer pageSize) {
		SupplierDTO to = new SupplierDTO();
		to.setName(searchKey);
		to.setPageNo(pageNo);
		to.setPageSize(pageSize);
		to.setOrgId(orgId);
		return getSqlMapClientTemplate().queryForList("selectPagedSuppliers", to);
	}

	@Override
	public List<SupplierDTO> retrieveSupplierBasicInfo(Integer orgId, String searchKey) {
		HashMap param = new HashMap();
		param.put("name", searchKey);
		param.put("orgId", orgId);
		return getSqlMapClientTemplate().queryForList("selectSupplierBasicInfo", param);
	}

	@Override
	public Integer retrieveSuppliersCount(Integer orgId, String searchKey) {
		SupplierDTO to = new SupplierDTO();
		to.setName(searchKey);	
		to.setOrgId(orgId);
		return (Integer) getSqlMapClientTemplate().queryForObject("selectSuppliersCount", to);
	}

	@Override
	public SupplierDTO updateSupplierDTO(SupplierDTO to)
			throws NoRecordUpdateException {
		int rows = getSqlMapClientTemplate().update("updateSupplier",to);
		if(rows < 1)
			throw new NoRecordUpdateException();
		return to;
	}

	@Override
	public boolean hasSupplier(Integer orgId, Integer id, String name) {
		HashMap param = new HashMap();
		param.put("id", id);
		param.put("name", name);
		param.put("orgId", orgId);
		Integer rowCount = (Integer) getSqlMapClientTemplate().queryForObject("selectSupplierByName", param);
		return (rowCount > 0);
	}

}
